package com.yeyaomai.dksns.flowis.service.result;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.yeyaomai.dksns.flowis.domain.Engine;
import com.yeyaomai.dksns.flowis.util.CollectionNames;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

/**
 * 生成动态XMl类
 * 
 * @author sun_shan
 * 
 */
public class EngineXml {
	
	//搜索引擎统计
	public static String getPie(List<Engine> engineList,String fromDate,String toDate){ 
		StringBuffer xml=new StringBuffer("");
		String betweentime=toDate+" / "+toDate;
		if(null!=engineList){
			String[] colors={"#628c33","#ff00c0","#CC3300","#945194"};
			xml.append("<chart showFCMenuItem='0' toolTipSepChar=': ' numberScaleUnit=' , ,万,千万' formatNumberScale='1' numberScaleValue='10000,1,1,1000' palette='4' bgColor='666666' showPercentageValues='0' use3DLighting='1' chartsshowShadow='0' subcaption='"+betweentime+"' caption='搜索引擎-按搜索次数查询' showShadow='0' bgAlpha='0' baseFontSize='12' baseFontColor='000000' bgAngle='360' showBorder='0'>");
			for(int i=0;i<engineList.size();i++){
				Engine engine=(Engine)engineList.get(i);
				if(engine.getEngineName().equals(CollectionNames.BAIDU)){
					xml.append("<set value='"+engine.getSearchTime()+"' label='"+engine.getEngineName()+"' color='#0f94d8' />");	 
				}else if(engine.getEngineName().equals(CollectionNames.SOUGOU)){
					xml.append("<set value='"+engine.getSearchTime()+"' label='"+engine.getEngineName()+"' color='#fff20f' />");	 
				}else if(engine.getEngineName().equals(CollectionNames.GUGE)){
					xml.append("<set value='"+engine.getSearchTime()+"' label='"+engine.getEngineName()+"' color='#a2161b' />");	 
				}else{
					xml.append("<set value='"+engine.getSearchTime()+"' label='"+engine.getEngineName()+"' color='"+colors[i]+"' />");	 					
				}				
			}
			xml.append("<styles>");
			xml.append("<definition>");
			xml.append("<style name='CaptionFont' type='font' bold='0' size='12'/>");
			xml.append("</definition>");
			xml.append("<application>");
			xml.append("<apply toObject='subcaption' styles='CaptionFont'/>");
			xml.append("</application>");
			xml.append("</styles>");
			xml.append("</chart>");
		}
		return xml.toString();
	}
	
	//主要搜索引擎搜索量近30日趋势 
	public static String getLine(Map<String, Long> baiduMap,Map<String, Long> sougouMap,Map<String, Long> gugeMap){ 
		StringBuffer xml=new StringBuffer("");
		xml.append("<chart showFCMenuItem='0' baseFontSize='12' outCnvbaseFontSize='10' legendShadow='0' numberScaleUnit=' , ,万,千万' formatNumberScale='1' numberScaleValue='10000,1,1,1000' lineThickness='1' showValues='0' anchorRadius='1' divLineAlpha='20' divLineColor='CC3300' divLineIsDashed='1' showAlternateHGridColor='1' alternateHGridAlpha='5' alternateHGridColor='CC3300' shadowAlpha='40' labelStep='1' numvdivlines='1' chartsshowShadow='0' chartRightMargin='20' chartTopMargin='15' chartLeftMargin='0' chartBottomMargin='2' bgColor='FFFFFF' legendBorderAlpha='0' bgAngle='360' showlegend='1' borderColor='DEF3F3' canvasBorderThickness='1' lineColor='AFD8F8' >");
		xml.append("<categories>");
		
		List<Map.Entry<String, String>> map=VelocityToolDateUtils.getThirtydayList();
		for(Map.Entry<String, String> entry:map){
			String x="0";
			if(Integer.parseInt(entry.getKey())%7==0){ 
				x="1";
			}
			xml.append("<category label='"+entry.getValue()+"' showLabel='"+x+"'/>");
		}	
		xml.append("</categories>");
			
		xml.append("<dataset  seriesName='百度' color='#0f94d8' anchorBorderColor='#0f94d8' anchorBgColor='#0f94d8'>");
		for(Map.Entry<String, Long> entry:baiduMap.entrySet()){
			xml.append("<set  value='"+entry.getValue()+"' toolText='百度: "+entry.getValue()+" "+entry.getKey()+"'  />");
		}
		xml.append("</dataset>");
		
		xml.append("<dataset  seriesName='搜狗' color='#fff20f' anchorBorderColor='#fff20f' anchorBgColor='#fff20f'>");
		for(Map.Entry<String, Long> entry:sougouMap.entrySet()){			
			xml.append("<set  value='"+entry.getValue()+"' toolText='搜狗: "+entry.getValue()+" "+entry.getKey()+"'  />");
		}
		xml.append("</dataset>");
		
		xml.append("<dataset  seriesName='谷歌' color='#a2161b' anchorBorderColor='#a2161b' anchorBgColor='#a2161b'>");
		for(Map.Entry<String, Long> entry:gugeMap.entrySet()){			
			xml.append("<set  value='"+entry.getValue()+"' toolText='谷歌: "+entry.getValue()+" "+entry.getKey()+"'  />");
		}
		xml.append("</dataset>");
		
		xml.append("<styles>");
		xml.append("<definition>");
		xml.append("<style name='CaptionFont' type='font' size='12'/>");
		xml.append("<style name='myLegendFont' type='font' size='12'/>");
		xml.append("</definition>");
		xml.append("<application>");
		xml.append("<apply toObject='CAPTION' styles='CaptionFont'/>");
		xml.append("<apply toObject='SUBCAPTION' styles='CaptionFont'/>");
		xml.append("<apply toObject='Legend' styles='myLegendFont'/>");
		xml.append("</application>");
		xml.append("</styles>");
		xml.append("</chart>");
		return xml.toString();
	}
	
	//总搜索量近30日趋势 
	public static String getArea(Map<String,Long> engineMap){
		StringBuffer xml=new StringBuffer("");
		xml.append("<chart showFCMenuItem='0' baseFontSize='12' outCnvbaseFontSize='10' numberScaleUnit=' , ,万,千万' formatNumberScale='1' numberScaleValue='10000,1,1,1000' bgColor='ffffff' bgborder='0' outCnvBaseFontColor='666666' showValues='0' numVDivLines='0' showAlternateVGridColor='0' showlegend='0' baseFontColor='000000' chartsshowShadow='0' canvasBorderThickness='0' borderColor='DEF3F3' showPlotBorder='0' plotFillAlpha='80' chartLeftMargin='0' chartRightMargin='20' areaBgColor='AFD8F8'>");
		xml.append("<categories>");
		//坐标数据
		List<Map.Entry<String, String>> map=VelocityToolDateUtils.getThirtydayList();
		for(Map.Entry<String, String> entry:map){
			String x="0";
			if(Integer.parseInt(entry.getKey())%7==0){ 
				x="1";
			}
			xml.append("<category label='"+entry.getValue()+"' showLabel='"+x+"'/>");
		}
		xml.append("</categories>");
		xml.append("<dataset  seriesName='总浏览次数' color='49acff' plotBorderColor='0067be'>");
		//图表数据
		for(Entry<String,Long> entry:engineMap.entrySet()){
			xml.append("<set  value='"+entry.getValue()+"' toolText='总搜索量: "+entry.getValue()+"\n"+entry.getKey()+"'/>");
		}
		xml.append("</dataset>");
		xml.append("<styles>");
		xml.append("<definition>");
		xml.append("<style name='TrendAnim' type='animation' param='_alpha' duration='1' start='1'/>");
		xml.append("</definition>");
		xml.append("<application>");
		xml.append("<apply toObject='TRENDLINES' styles='TrendAnim'/>");
		xml.append("</application>");
		xml.append("</styles>");
		xml.append("</chart>");
		return xml.toString();
	}

}
